package com.lxk.concurrency.example.atomic;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import lombok.Getter;

import com.lxk.concurrency.annoations.ThreadSafe;

@ThreadSafe
public class AtomicExample5 {

	// 修改某个类中的指定字段的值
	private static AtomicIntegerFieldUpdater<AtomicExample5> updater = AtomicIntegerFieldUpdater.newUpdater(AtomicExample5.class, "count");

	@Getter
	// 这个类中的字段，只能为 volatile，且不能是 static
	public volatile int count = 100;

	private static AtomicExample5 example5 = new AtomicExample5();

	public static void main(String[] args) {

		if (updater.compareAndSet(example5, 100, 120)) {

			System.out.println("update success 1:" + example5.getCount());

		}

		if (updater.compareAndSet(example5, 100, 120)) {

			System.out.println("update success 2:" + example5.getCount());

		} else {
			System.out.println("update failed:" + example5.getCount());
		}

	}
}
